#region Script Settings
#<ScriptSettings xmlns="http://tempuri.org/ScriptSettings.xsd">
#  <ScriptPackager>
#    <process>powershell.exe</process>
#    <arguments />
#    <extractdir>%TEMP%</extractdir>
#    <files />
#    <usedefaulticon>true</usedefaulticon>
#    <showinsystray>false</showinsystray>
#    <altcreds>false</altcreds>
#    <efs>true</efs>
#    <ntfs>true</ntfs>
#    <local>false</local>
#    <abortonfail>true</abortonfail>
#    <product />
#    <version>1.0.0.1</version>
#    <versionstring />
#    <comments />
#    <company />
#    <includeinterpreter>false</includeinterpreter>
#    <forcecomregistration>false</forcecomregistration>
#    <consolemode>false</consolemode>
#    <EnableChangelog>false</EnableChangelog>
#    <AutoBackup>false</AutoBackup>
#    <snapinforce>false</snapinforce>
#    <snapinshowprogress>false</snapinshowprogress>
#    <snapinautoadd>2</snapinautoadd>
#    <snapinpermanentpath />
#    <cpumode>1</cpumode>
#    <hidepsconsole>false</hidepsconsole>
#  </ScriptPackager>
#</ScriptSettings>
#endregion

#
# This is a temporary script that removes all the queues created by PubSubTest tool
#


[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
$msmq = [System.Messaging.MessageQueue]

foreach ($q in $msmq::GetPrivateQueuesByMachine("."))
{
    #if ($q.QueueName -match '.*pub\d.*')
    if (($q.QueueName -match '.*pubsubtester') -or ($q.QueueName -match '.*rockbus'))
    {
        echo("Removing    " + $q.QueueName)
        try {			
            $msmq::Delete(".\" + $q.QueueName)
        }
        catch
        {
            <#
            You can have multiple catch blocks(for different exceptions), or one single catch.
            The last error record is available inside the catch block under the $_ variable.
            Code inside this block is used for error handling.Examples include logging an error, 
            sending an email, writing to the event log, performing a recovery action, etc.
            in this example I 'm just printing the exception type and message to the screen.
            #>			 
            write-host "Caught an exception:" -ForegroundColor Red
            write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
            write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
            }
    }	
}
echo "Deleting complete"

echo "Remaining queues:"
foreach ($q in $msmq::GetPrivateQueuesByMachine("."))
{
    echo("    " + $q.QueueName)	
        
}
echo "Done."